package com.guc.md

import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.core.view.GravityCompat
import com.guc.gviewbinding.inflate
import com.guc.md.databinding.ActivityMainBinding
import com.guc.md.utils.SnackbarUtil

/**
 * Created by Guc on 2021/5/21.
 * Description：侧滑
 */
class MainActivity : AppCompatActivity() {
    private val binding: ActivityMainBinding by inflate()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setSupportActionBar(binding.mainLayout.toolbar)
        supportActionBar?.run { //显示导航按钮
            setDisplayHomeAsUpEnabled(true)
            setHomeAsUpIndicator(R.drawable.icon_menu)
        }
        initDrawer()
        initFloatActionButton()
    }

    private fun initFloatActionButton() {
        binding.mainLayout.tip.setOnClickListener {
//            Toast.makeText(this, "欢迎蜡笔小新O(∩_∩)O~", Toast.LENGTH_SHORT).show()
            SnackbarUtil.ShortSnackbar(it, "欢迎蜡笔小新O(∩_∩)O~", SnackbarUtil.ALERT).setAction("更多") {
                Toast.makeText(
                    this,
                    "《蜡笔小新》是由日本漫画家臼井仪人创作的漫画。1990年8月，在《weekly漫画action》上开始连载。",
                    Toast.LENGTH_SHORT
                ).show()
            }.setActionTextColor(ActivityCompat.getColor(this, R.color.white)).show()
        }
        binding.mainLayout.cardStory.setOnClickListener {
            val optionsCompat =
                ActivityOptionsCompat.makeScaleUpAnimation(it, 0, 0, 0, 0)
            ActivityCompat.startActivity(
                this,
                Intent(this, StoryActivity::class.java),
                optionsCompat.toBundle()
            )
        }
    }

    private fun initDrawer() {
        binding.navigationView.apply {
            setCheckedItem(R.id.profile)
            setNavigationItemSelectedListener {
                //关闭滑动菜单
                when (it.itemId) {
                    R.id.profile -> Toast.makeText(this@MainActivity, "点击了简介菜单", Toast.LENGTH_SHORT)
                        .show()
                    R.id.settings -> Toast.makeText(
                        this@MainActivity,
                        "点击了设置菜单",
                        Toast.LENGTH_SHORT
                    ).show()
                }
                binding.drawer.closeDrawers()
                return@setNavigationItemSelectedListener true
            }
            this.getHeaderView(0).apply {
                findViewById<ImageView>(R.id.icon).setOnClickListener {
                    Toast.makeText(this@MainActivity, "点击了头像", Toast.LENGTH_SHORT)
                        .show()
                }
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.add -> Toast.makeText(this, "点击了新增按钮", Toast.LENGTH_SHORT).show()
            R.id.set -> Toast.makeText(this, "点击了设置按钮", Toast.LENGTH_SHORT).show()
            android.R.id.home -> {
                binding.drawer.openDrawer(GravityCompat.START)
            }
        }
        return super.onOptionsItemSelected(item)
    }
}